package com.xuecheng.media.controller;

import com.xuecheng.commons.model.dto.MediaDto;
import com.xuecheng.commons.model.dto.MediaQueryDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.VodUploadDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResourceVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.Media;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService  mediaService;

    //上传凭证
    @PostMapping("vod-token")
    public ResponseResult createToken (@RequestBody VodUploadDto dto) throws Exception {
        return mediaService.createToken(dto);
    }

    //更新凭证
    @GetMapping("refresh-vod-token/{videoId}")
    public ResponseResult refreshToken (@PathVariable("videoId") String videoId) throws Exception {
        return mediaService.refreshToken(videoId);
    }

    //保存媒资
    @PostMapping
    public ResponseResult saveMedia (@RequestBody MediaDto dto) throws Exception {
        return mediaService.saveMedia(dto);
    }

    //分页查询媒资数据
    @PostMapping("list")
    public ResponseResult findMediaList (PageRequest request,@RequestBody MediaQueryDto dto) {
        return mediaService.findMediaList(request,dto);
    }

    //根据id查询媒资
    @GetMapping("{mediaId}")
    public ResponseResult findById (@PathVariable("mediaId") Long mediaId) {
        Media media = mediaService.getById(mediaId);
        MediaVo vo = BeanHelper.copyProperties(media, MediaVo.class);
        return ResponseResult.okResult(vo);
    }

    //根据媒资id获取播放连接
    @GetMapping("preview/{mediaId}")
    public ResponseResult previewMediaById (@PathVariable("mediaId") Long mediaId) throws Exception {
        return mediaService.previewMediaById(mediaId);
    }

    //根据id删除媒资信息
    @DeleteMapping("{mediaId}")
    public ResponseResult deleteMediaById (@PathVariable("mediaId") Long mediaId) {
        return mediaService.deleteMediaById(mediaId);
    }

    //根据id获取媒资的播放地址
    @GetMapping("media-url/{mediaId}")
    public ResponseResult<ResourceVo> findMediaUrl (@PathVariable("mediaId") Long mediaId) throws Exception {
        return mediaService.findMediaUrl(mediaId);
    }
}
